﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.DA;
using System.Windows;
using System.Collections.ObjectModel;

namespace Com.BU
{
    public class LibraryManagerInCom
    {
        #region Khai bao List
        // Khai báo đối tượng data model EcoSystemEntities
        public static EcoSystemEntities eco = new EcoSystemEntities();
        //  Khai báo list các địa điểm lắp đặt
        public static ObservableCollection<tb_location> LocationList;
        //  Khai báo list các loại bao bì
        public static ObservableCollection<tb_wrapping> WrappingList;
        //  Khai báo list dữ liệu sản phầm
        public static ObservableCollection<tb_product> ProductList;
        //  Khai báo List dữ liệu các thông số entanpy
        public static ObservableCollection<tb_enthalpy> ProductEnthalpyList;
        //  Khai báo list dữ liệu của hướng kính và các thông số có liên quan
        public static ObservableCollection<tb_glass_direction> GlassDirectionList;
        public static ObservableCollection<tb_immediate_impact> ImmediateImpactList;
        public static ObservableCollection<tb_solar_radiation_through_glass> SolarRadiationList;
        //  Khai báo list dữ liệu của kính tường
        public static ObservableCollection<tb_wall_glass_heat_coefficient> WallGlassCoefficientList;
        //  Khai báo list các loại kính và cacs hệ số bức xạ
        public static ObservableCollection<tb_radiation_characteristics> GlassCoeffientList;
        //  Khai báo list hệ số truyền nhiệt của trần mái 
        public static ObservableCollection<tb_roof_ceiling_heat_coefficient> RoofCeilingHeatList;
        //  Khai báo list các đặc tính của trần mái
        public static ObservableCollection<tb_roof_ceiling_characteristic> RoofCeilingCharacteristicList;
        //  Khai báo list các loại cấu trúc của trần mái
        public static ObservableCollection<tb_roof_ceiling_structure> RoofCeilingStructureList;
        //  Khai báo list hệ số hấp thụ bức xạ mặt trời của bề mặt amis
        public static ObservableCollection<tb_roof_surface> RoofSurfaceList;
        //  Khai báo list các loại trần
        public static ObservableCollection<tb_ceiling> CeilingList;
        //  Khai báo list các đặc điểm mặt trên của trần
        public static ObservableCollection<tb_ceiling_layer> CeilingLayerList;
        // Khai báo list các thông số truyền nhiệt của trần
        public static ObservableCollection<tb_heat_transfer_ceiling> CeilingCoefficientList;
        // Khai báo list các thông số truyền nhiệt của kính cửa sổ và giếng trời
        public static ObservableCollection<tb_window_skywell> WindowSkywellCoefficientList;
        // Khai báo list các thông số truyền nhiệt của cửa gỗ và kính tường
        public static ObservableCollection<tb_wood_door_heat_coefficient> WoodGlassCoefficientList;
        // Khai báo list các vật liệu làm tường và thông số nhiệt trở của chúng
        public static ObservableCollection<tb_heat_resistance> WallMaterialList;
        //  Khai báo list các không gian điều hòa
        public static ObservableCollection<tb_air_conditioning_place> AirConditionPlaceList;
        //  Khai báo list các chế độ làm việc của người
        public static ObservableCollection<tb_working_level> WorkingLevelList;
        // Khai báo list các dữ liệu nhiệt tỏa ra do cơ thể người
        public static ObservableCollection<tb_heat_by_man> HeatByManList;
        // Khai báo list hệ số không khí thóat
        public static ObservableCollection<tb_exiting_air> ExitAirList;
        //  Khai báo list các loại buồng lạnh
        public static ObservableCollection<tb_storage_room_type> RoomTypeList;
        //  Khai báo các loại tường của buồng lạnh và hiệu nhiệt độ dư
        public static ObservableCollection<tb_temperature_excess> ColdStorageWallList;
        //  Khai báo các dữ liệu chất tải tiêu chuẩn của buồng lạnh đối với từng loái sản phẩm
        public static ObservableCollection<tb_loading_standard> LoadingStandardList;
        public static ObservableCollection<tb_wall_material> CSWallMaterialList;
        public static ObservableCollection<tb_ceiling_type> CeilingTypeList;
        public static ObservableCollection<tb_wall_type> wallTypeList;
        public static ObservableCollection<tb_wall_direction> wallDirectionList;
        public static ObservableCollection<tb_ceiling_type> RoofList;
        public static ObservableCollection<tb_respiratory_heat> RespirationList;
        public static ObservableCollection<tb_temperature_excess> TemperatureExcessList;
        public static ObservableCollection<tb_equipment> EquipmentList;

        public static ObservableCollection<tb_water_coefficients> WaterCoefficientsList;
        public static ObservableCollection<tb_air_coefficients> AirCoefficientsList;
        #endregion

        #region LOAD
        public static void loadroofType()
        {
            RoofList = new ObservableCollection<tb_ceiling_type>(eco.tb_ceiling_type.ToList());
        }

        public static void loadWallType()
        {
            wallTypeList = new ObservableCollection<tb_wall_type>(eco.tb_wall_type.ToList());
        }

        public static void loadDirection()
        {
            wallDirectionList = new ObservableCollection<tb_wall_direction>(eco.tb_wall_direction.ToList());
        }

        public static void loadRespirationList()
        {
            RespirationList = new ObservableCollection<tb_respiratory_heat>(eco.tb_respiratory_heat.ToList());
        }

        public static void loadColdStorageWallList()
        {
            ColdStorageWallList = new ObservableCollection<tb_temperature_excess>(eco.tb_temperature_excess.ToList());
        }

        public static void loadCSWallMaterialList()
        {
            CSWallMaterialList = new ObservableCollection<tb_wall_material>(eco.tb_wall_material.ToList());
        }

        public static void loadCeilingTypeList()
        {
            CeilingTypeList = new ObservableCollection<tb_ceiling_type>(eco.tb_ceiling_type.ToList());
        }

        public static void loadTemperatureExcessList()
        {
            TemperatureExcessList = new ObservableCollection<tb_temperature_excess>(eco.tb_temperature_excess.ToList());
        }

        public static void loadTypeOfGlass()
        {
            GlassCoeffientList = new ObservableCollection<tb_radiation_characteristics>(eco.tb_radiation_characteristics.ToList());
        }

        //  Load hướng của tường
        public static void loadDirectionOfGlass()
        {
            GlassDirectionList = new ObservableCollection<tb_glass_direction>(eco.tb_glass_direction.ToList());

        }

        //  Load số Pressure kg/m2 sàn
        public static void loadPressure()
        {
            ImmediateImpactList = new ObservableCollection<tb_immediate_impact>(eco.tb_immediate_impact.ToList());
        }

        // Load thời gian
        public static void loadTime()
        {
            ImmediateImpactList = new ObservableCollection<tb_immediate_impact>(eco.tb_immediate_impact.ToList());
        }

        // Load lượng bức xạ mặt trời lớn nhất qua kính
        public static void loadSolarByRadiation()
        {
            SolarRadiationList = new ObservableCollection<tb_solar_radiation_through_glass>(eco.tb_solar_radiation_through_glass.ToList());
        }

        //  Load cấu tạo của trần
        public static void loadCeilling()
        {
            CeilingList = new ObservableCollection<tb_ceiling>(eco.tb_ceiling.ToList());
        }

        //  Load đặc điểm của trần
        public static void loadCeillingLayer()
        {
            CeilingLayerList = new ObservableCollection<tb_ceiling_layer>(eco.tb_ceiling_layer.ToList());
        }

        //  Load cấu tạo của mái
        public static void loadStructureRoof()
        {
            RoofCeilingStructureList = new ObservableCollection<tb_roof_ceiling_structure>(eco.tb_roof_ceiling_structure.ToList());
        }

        //  Load đặc điểm của mái
        public static void loadCharacteristicRoof()
        {
            RoofCeilingCharacteristicList = new ObservableCollection<tb_roof_ceiling_characteristic>(eco.tb_roof_ceiling_characteristic.ToList());
        }

        //  Load loại mặt mái
        public static void loadRoofSurface()
        {
            RoofSurfaceList = new ObservableCollection<tb_roof_surface>(eco.tb_roof_surface.ToList());
        }

        //  Load vị trí   
        public static void loadLocation()
        {
            LocationList = new ObservableCollection<tb_location>(eco.tb_location.ToList());
        }

        public static void loadWrapping()
        {
            WrappingList = new ObservableCollection<tb_wrapping>(eco.tb_wrapping.ToList());
        }

        //  Gán dữ liệu product trong database cho ProductList
        public static void loadProduct()
        {
            ProductList = new ObservableCollection<tb_product>(eco.tb_product.ToList());
        }

        // Gán dữ liệu enthalpy trong database cho ProductEnthalpyList
        public static void loadEnthapyList()
        {
            ProductEnthalpyList = new ObservableCollection<tb_enthalpy>(eco.tb_enthalpy.ToList());
        }

        // Gán dữ liệu loaddingStandard trong database cho LoadingStandardList
        public static void loadLoadingStandardList()
        {
            LoadingStandardList = new ObservableCollection<tb_loading_standard>(eco.tb_loading_standard.ToList());
        }

        // Gán dữ liệu hướng của tường trong database cho GlassDirectionList
        public static void loadGlassDirection()
        {
            GlassDirectionList = new ObservableCollection<tb_glass_direction>(eco.tb_glass_direction.ToList());
        }

        //  Gán dữ liệu các hệ số tác động tức thời trong database cho ImmediateImpactList
        public static void loadImmediateImpact()
        {
            ImmediateImpactList = new ObservableCollection<tb_immediate_impact>(eco.tb_immediate_impact.ToList());
        }

        //  Gán dữ liệu các hệ số bức xạ mặt trời trong cơ sở dữ liệu ccho SolarRadiationList
        public static void loadSolarRadiation()
        {
            SolarRadiationList = new ObservableCollection<tb_solar_radiation_through_glass>(eco.tb_solar_radiation_through_glass.ToList());
        }

        // Gán dữ liệu
        public static void loadWallGlassCoefficientList()
        {
            WallGlassCoefficientList = new ObservableCollection<tb_wall_glass_heat_coefficient>(eco.tb_wall_glass_heat_coefficient.ToList());
        }

        // Gán dữ liệu các loại kính và hệ số bức xạ cho GlassCoefficientList
        public static void loadGlassList()
        {
            GlassCoeffientList = new ObservableCollection<tb_radiation_characteristics>(eco.tb_radiation_characteristics.ToList());
        }

        // Gán dữ liệu hệ số truyền nhiệt của trần mái cho RoofCeilingHeatList
        public static void loadRoofCeilingCoefficient()
        {
            RoofCeilingHeatList = new ObservableCollection<tb_roof_ceiling_heat_coefficient>(eco.tb_roof_ceiling_heat_coefficient.ToList());
        }

        // Gán dữ liệu đặc điểm cấu tạo của trần mái cho RoofCeilingStructureList
        public static void loadRoofCeilingStructure()
        {
            RoofCeilingStructureList = new ObservableCollection<tb_roof_ceiling_structure>(eco.tb_roof_ceiling_structure);
        }

        // Gán dữ liệu hệ số hấp thu bức xạ mặt trời của bề mặt mái cho RoofSurfaceList
        public static void loadSurfaceCoefficient()
        {
            RoofSurfaceList = new ObservableCollection<tb_roof_surface>(eco.tb_roof_surface.ToList());
        }

        // Gán dữ liệu các loại mái cho Ceilinglist
        public static void loadCeiling()
        {
            CeilingList = new ObservableCollection<tb_ceiling>(eco.tb_ceiling.ToList());
        }

        //  Gán dữ liệu đặc tính các lớp mái cho Ceilinglayerlist
        public static void loadCeilingLayer()
        {
            CeilingLayerList = new ObservableCollection<tb_ceiling_layer>(eco.tb_ceiling_layer.ToList());
        }

        //  Gán dữ liệu hệ số truyền nhiệt của mái
        public static void loadCeilingCoefficient()
        {
            CeilingCoefficientList = new ObservableCollection<tb_heat_transfer_ceiling>(eco.tb_heat_transfer_ceiling.ToList());
        }

        //  Gán dữ liệu các hệ số truyền nhiệt giữa kính cửa sổ và giếng trời cho Window Skywell CoefficientList
        public static void loadWindowSkywellCoefficient()
        {
            WindowSkywellCoefficientList = new ObservableCollection<tb_window_skywell>(eco.tb_window_skywell.ToList());
        }

        // Gán dữ liệu các hệ số truyền nhiệt của cửa kính và cửa foox cho WoodGlassCoefficientList
        public static void loadWoodGlassCoefficient()
        {
            WoodGlassCoefficientList = new ObservableCollection<tb_wood_door_heat_coefficient>(eco.tb_wood_door_heat_coefficient.ToList());
        }

        // Gán dữ liệu các chất liệu tường cho WallMaterialList
        public static void loadWallMaterial()
        {
            WallMaterialList = new ObservableCollection<tb_heat_resistance>(eco.tb_heat_resistance.ToList());
        }

        //  Gán dữ liệu các không gian điều hòa cho AirConditionPlacelist
        public static void loadAirConditionPlace()
        {
            AirConditionPlaceList = new ObservableCollection<tb_air_conditioning_place>(eco.tb_air_conditioning_place.ToList());
        }

        //  Gán các dữ liệu các chế độ làm việc cho Workinglevellist
        public static void loadWorkingLevel()
        {
            WorkingLevelList = new ObservableCollection<tb_working_level>(eco.tb_working_level.ToList());
        }

        //  Gán các dữ liệu nhiệt tỏa ra do người cho HeatByManList
        public static void loadHeatByMan()
        {
            HeatByManList = new ObservableCollection<tb_heat_by_man>(eco.tb_heat_by_man.ToList());
        }

        //  Gán các dữ liệu không khí lọt cho ExitAirList
        public static void loadExitingAir()
        {
            ExitAirList = new ObservableCollection<tb_exiting_air>(eco.tb_exiting_air.ToList());
        }

        public static void loadRoomType()
        {
            RoomTypeList = new ObservableCollection<tb_storage_room_type>(eco.tb_storage_room_type.ToList());
        }

        public static void loadEquipment()
        {
            EquipmentList = new ObservableCollection<tb_equipment>(eco.tb_equipment.ToList());
        }

        public static void loadLatitute()
        {
            SolarRadiationList = new ObservableCollection<tb_solar_radiation_through_glass>(eco.tb_solar_radiation_through_glass.ToList());
        }

        //  Load giờ mặt trời
        public static void loadSolarHour()
        {
            SolarRadiationList = new ObservableCollection<tb_solar_radiation_through_glass>(eco.tb_solar_radiation_through_glass.ToList());
        }

        //  Load tháng
        public static void loadMonth()
        {
            SolarRadiationList = new ObservableCollection<tb_solar_radiation_through_glass>(eco.tb_solar_radiation_through_glass.ToList());
        }

        // Gán dữ liệu đặc tính của trần mái cho RoofCeilingCharacteristicList
        public static void loadRoofCeilingCharacteristic()
        {
            RoofCeilingCharacteristicList = new ObservableCollection<tb_roof_ceiling_characteristic>(eco.tb_roof_ceiling_characteristic.ToList());
        }
        #endregion

        #region LIBRARY MANAGER

        #region COLD STORAGE LIBRARY

        #region Location Library
        // Thêm đối tượng location vào database
        public static void addLocation(tb_location lc)
        {
            try
            {
                tb_location lc2 = LocationList.SingleOrDefault(i => i.location == lc.location);
                if (lc2 == null)
                {

                    eco.tb_location.AddObject(lc);
                    eco.SaveChanges();
                    LocationList.Add(lc);
                    MessageBox.Show("Add new a location successfully!");
                }
                else
                {
                    lc2.average_humidity = lc.average_humidity;
                    lc2.average_temperature = lc.average_temperature;
                    lc2.max_humidity = lc.max_humidity;
                    lc2.max_temperature = lc.max_temperature;
                    eco.SaveChanges();
                    MessageBox.Show("This item is alreasy exist!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        // Xóa đối tượng location được chọn trong database
        // Nếu ko còn đối tượng nào để xóa, sẽ hiện thông báo cho người dùng
        public static void delLocation(tb_location loc)
        {
            if (eco.tb_location == null)
            {
                MessageBox.Show("No location is available");
            }
            else
            {
                eco.tb_location.DeleteObject(loc);
                eco.SaveChanges();
                LocationList.Remove(loc);
                MessageBox.Show("Delete a location successfully");
            }

        }

        //  Khởi tạo 1 đối tượng location mới
        //  và copy các thuộc tính của đối tượng được chọn vào đối tượng mới   
        public static tb_location copyLocation(tb_location lc)
        {
            tb_location lc2 = new tb_location();
            lc2.location = "Location -- " + (eco.tb_location.Count() + 1);
            lc2.max_humidity = lc.max_humidity;
            lc2.max_temperature = lc.max_temperature;
            lc2.average_humidity = lc.average_humidity;
            lc2.average_temperature = lc.average_temperature;

            return lc2;
        }
        #endregion

        #region Product Library
        // Thêm đối tượng Product mới vào cơ sở dữ liệu
        public static void addProduct(tb_product pr)
        {
            tb_product pr2 = ProductList.SingleOrDefault(i => i.product_name == pr.product_name);
            if (pr2 == null)
            {
                eco.tb_product.AddObject(pr);
                eco.SaveChanges();
                ProductList.Add(pr);
                MessageBox.Show("Add new a product item successfully!");
            }
            else
            {
                pr2.product_type = pr.product_type;
                pr2.specific_heat = pr.specific_heat;
                eco.SaveChanges();
            }
        }

        //  Xóa đối tượng Product trong cơ sở dữ liệu
        public static void delProduct(tb_product pr)
        {
            MessageBoxResult rs = MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
            if (rs == MessageBoxResult.OK)
            {
                if (eco.tb_product == null)
                {
                    MessageBox.Show("No product item is available");
                }
                else
                {
                    foreach (tb_enthalpy en in eco.tb_enthalpy)
                    {
                        if (en.product_id == pr.id)
                        {
                            eco.tb_enthalpy.DeleteObject(en);
                            ProductEnthalpyList.Remove(en);
                        }
                    }

                    foreach (tb_loading_standard ls in eco.tb_loading_standard)
                    {
                        if (ls.product_id == pr.id)
                        {
                            eco.tb_loading_standard.DeleteObject(ls);
                            LoadingStandardList.Remove(ls);
                        }
                    }

                    eco.tb_product.DeleteObject(pr);
                    eco.SaveChanges();
                    ProductList.Remove(pr);
                    MessageBox.Show("Delete a product item successfully");
                }
            }

        }

        //  Khởi tạo một đối tượng Product mới
        // Sao chép các thuộc tính của đối tượng Product được chọn cho đối tượng Product mới tạo
        public static tb_product copyProduct(tb_product pr)
        {
            tb_product pr2 = new tb_product();
            pr2.product_name = "Product -- " + (eco.tb_product.Count() + 1);
            pr2.product_type = pr.product_type;
            pr2.specific_heat = pr.specific_heat;

            return pr2;
        }
        #endregion

        #region Enthanpy Library
        public static void addEnthalpy(tb_enthalpy en)
        {
            try
            {
                tb_enthalpy en2 = ProductEnthalpyList.SingleOrDefault(i => i.product_id == en.product_id && i.temperature == en.temperature);
                if (en2 == null)
                {
                    eco.tb_enthalpy.AddObject(en);
                    eco.SaveChanges();
                    ProductEnthalpyList.Add(en);
                    MessageBox.Show("Add a new product enthalpy item successfully!");

                }
                else
                {
                    en2.enthalpy = en.enthalpy;
                    en2.product_id = en.product_id;
                    en2.temperature = en.temperature;
                    eco.SaveChanges();
                    MessageBox.Show("This item is already exits!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static void delEnthalpy(tb_enthalpy en)
        {
            MessageBoxResult rs = MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
            if (rs == MessageBoxResult.OK)
            {
                try
                {
                    eco.tb_enthalpy.DeleteObject(en);
                    eco.SaveChanges();
                    ProductEnthalpyList.Remove(en);
                    MessageBox.Show("Delete a loading standard successfully!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        public static tb_enthalpy copyEnthalpy(tb_enthalpy en)
        {
            tb_enthalpy en2 = new tb_enthalpy();
            en2.description = "Enthalpy -- " + (ProductList.Count() + 1);
            en2.product_id = en.product_id;
            en2.temperature = en.temperature;
            en2.enthalpy = en.enthalpy;

            return en2;
        }
        #endregion

        #region Loading Standard Library
        public static void addLoadingStandard(tb_loading_standard ls)
        {
            try
            {
                tb_loading_standard ls2 = LoadingStandardList.SingleOrDefault(i => i.product_id == ls.product_id);
                if (ls2 == null)
                {
                    eco.tb_loading_standard.AddObject(ls);
                    eco.SaveChanges();
                    LoadingStandardList.Add(ls);
                    MessageBox.Show("Add a new loading standard item successfully!");

                }
                else
                {
                    ls2.product_id = ls.product_id;
                    ls2.loading_standard = ls.loading_standard;
                    eco.SaveChanges();
                    MessageBox.Show("This item is already exits!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static void delLoadingStandard(tb_loading_standard ls)
        {
            MessageBoxResult rs = MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
            if (rs == MessageBoxResult.OK)
            {
                try
                {
                    eco.tb_loading_standard.DeleteObject(ls);
                    eco.SaveChanges();
                    LoadingStandardList.Remove(ls);
                    MessageBox.Show("Delete a loading standard successfully!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

        }

        public static tb_loading_standard copyLoadingStandard(tb_loading_standard ls)
        {
            tb_loading_standard ls2 = new tb_loading_standard();
            LoadingStandardList.Add(ls2);
            return ls2;
        }
        #endregion

        #region Cold Wall Library
        public static void addTemperatureExcessList(tb_temperature_excess te)
        {
            if (te.description.Equals(""))
            {
                string walltype = LibraryManagerInCom.CSWallMaterialList.SingleOrDefault(w => w.id == te.wall_material_id).wall_type;
                string dir = LibraryManagerInCom.wallDirectionList.SingleOrDefault(d => d.id == te.direction_id).direction;
                te.description = walltype + ", " + dir;
            }
            eco.tb_temperature_excess.AddObject(te);
            eco.SaveChanges();
            TemperatureExcessList.Add(te);
            MessageBox.Show("Add a temperature excess item successfully!");
        }

        public static void delTemperatureExcess(tb_temperature_excess te)
        {
            eco.DeleteObject(te);
            eco.SaveChanges();
            TemperatureExcessList.Remove(te);
            MessageBox.Show("Delete a temperature excess item successfully");
        }

        //  Sao chep doi tuong tb_temperautre_excess duoc nhap vao va tra ra doi tuong moi
        public static tb_temperature_excess CopyTemperatureExcess(tb_temperature_excess t1)
        {
            tb_temperature_excess t2 = new tb_temperature_excess();
            t2.wall_material_id = t1.wall_material_id;
            t2.direction_id = t1.direction_id;
            t2.termperature_excess = t1.termperature_excess;
            t2.description = " Temperature Excess -- " + (eco.tb_temperature_excess.Count() + 1);

            return t2;
        }

        #endregion

        #region Wrapping Library
        // Thêm đối tượng wrapping vào database
        public static void addWrapping(tb_wrapping wp)
        {
            eco.tb_wrapping.AddObject(wp);
            eco.SaveChanges();
            WrappingList.Add(wp);
            MessageBox.Show("Add new a wrapping item successfully!");
        }

        // Xóa đối tượng wrapping được chọn trong database
        // Nếu ko còn đối tượng nào để xóa, sẽ hiện thông báo cho người dùng
        public static void delWrapping(tb_wrapping wp)
        {
            if (eco.tb_wrapping == null)
            {
                MessageBox.Show("No wrapping item is available");
            }
            else
            {
                eco.tb_wrapping.DeleteObject(wp);
                eco.SaveChanges();
                WrappingList.Remove(wp);
                MessageBox.Show("Delete a wrapping item successfully");
            }

        }

        //  Khởi tạo một đối tượng wrapping mới
        //  và copy tất cả thuộc tính của đối tượng wrapping được chọn cho đối tượng vừa tạo
        public static tb_wrapping copyColdStorageWrapping(tb_wrapping wp)
        {
            tb_wrapping wp2 = new tb_wrapping();
            wp2.wrapping_name = "Wrapping -- " + (eco.tb_wrapping.Count() + 1);
            wp2.weight_rate = wp.weight_rate;
            wp2.specific_heat = wp.specific_heat;

            return wp2;
        }
        #endregion

        #region Room Type Library
        public static void addRoomType(tb_storage_room_type room, int volumeIndex)
        {
            try
            {
                tb_storage_room_type room2 = (from i in LibraryManagerInCom.RoomTypeList
                                              where i.storage_room == room.storage_room
                                              select i).SingleOrDefault();
                if (room2 == null)
                {
                    eco.tb_storage_room_type.AddObject(room);
                    eco.SaveChanges();
                    RoomTypeList.Add(room);
                    MessageBox.Show("Add a new storage room type successfully!");
                }
                else
                {
                    MessageBoxResult result = MessageBox.Show("This item is alreay exist!\n Do you want to update this item?", "Warning", MessageBoxButton.OKCancel, MessageBoxImage.Warning);

                    if (result == MessageBoxResult.OK)
                    {
                        if (volumeIndex == 0)
                        {
                            room2.C50m2 = room.C50m2;
                        }
                        if (volumeIndex == 1)
                        {
                            room2.C50_150m2 = room.C50_150m2;
                        }
                        if (volumeIndex == 2)
                        {
                            room2.C_150m2 = room.C_150m2;
                        }
                        MessageBox.Show("Update a room type item successful!");
                        eco.SaveChanges();
                    }


                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static void delRoomType(tb_storage_room_type room)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (result == MessageBoxResult.OK)
                {
                    eco.tb_storage_room_type.DeleteObject(room);
                    eco.SaveChanges();
                    RoomTypeList.Remove(room);
                    MessageBox.Show("Delete a storage room type successfully!");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static tb_storage_room_type copyRoomType(tb_storage_room_type room)
        {
            tb_storage_room_type room2 = new tb_storage_room_type();
            room2.storage_room = "Room -- " + (LibraryManagerInCom.RoomTypeList.Count() + 1).ToString();
            room2.C50m2 = room.C50m2;
            room2.C50_150m2 = room.C50_150m2;
            room2.C_150m2 = room.C_150m2;

            return room2;
        }
        #endregion

        #endregion

        #region AIR CONDITION LIBARY

        #region Solar By Radiation Library
        public static void addSolarByRadiation(tb_solar_radiation_through_glass so)
        {
            try
            {
                tb_solar_radiation_through_glass so1 = LibraryManagerInCom.SolarRadiationList.SingleOrDefault(i => i.description == so.description);
                if (so1 == null)
                {
                    eco.tb_solar_radiation_through_glass.AddObject(so);
                    eco.SaveChanges();
                    SolarRadiationList.Add(so);
                    MessageBox.Show("Add a new solar radiation through glass successfully");
                }
                else
                {
                    MessageBox.Show("This item is already exist!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static void delSolarByRadiation(tb_solar_radiation_through_glass so)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (result == MessageBoxResult.OK)
                {
                    eco.tb_solar_radiation_through_glass.DeleteObject(so);
                    eco.SaveChanges();
                    SolarRadiationList.Remove(so);
                    MessageBox.Show("Delete a solar radiation through glass successfully!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static tb_solar_radiation_through_glass copySolrByRadiation(tb_solar_radiation_through_glass so)
        {
            tb_solar_radiation_through_glass so2 = new tb_solar_radiation_through_glass();
            try
            {

                so2.description = "Solar radiation -- " + (LibraryManagerInCom.SolarRadiationList.Count() + 1).ToString();
                so2.direction_id = so.direction_id;
                so2.month = so.month;
                so2.solar_hour = so.solar_hour;
                so2.latitute = so.latitute;
                so2.radiation_amount = so.radiation_amount;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return so2;
        }
        #endregion

        #region Immediate Impact Library
        public static void addImmediateImpact(tb_immediate_impact im)
        {
            try
            {
                tb_immediate_impact im2 = LibraryManagerInCom.ImmediateImpactList.SingleOrDefault(i => i.description == im.description);
                if (im2 == null)
                {
                    eco.tb_immediate_impact.AddObject(im);
                    eco.SaveChanges();
                    ImmediateImpactList.Add(im);
                    MessageBox.Show("Add a new immediate impact item successfully!");
                }
                else
                {
                    MessageBox.Show("This item is already exist");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static void delImmediateImpact(tb_immediate_impact im)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("Do you want to delete this item?", "Warning", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (result == MessageBoxResult.OK)
                {
                    eco.tb_immediate_impact.DeleteObject(im);
                    eco.SaveChanges();
                    ImmediateImpactList.Remove(im);
                    MessageBox.Show("Delete a immediate impact item successfully!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public static tb_immediate_impact copyImmediateImpact(tb_immediate_impact im)
        {
            tb_immediate_impact im2 = new tb_immediate_impact();
            try
            {
                im2.description = "ImmediateImpact -- " + (LibraryManagerInCom.ImmediateImpactList.Count() + 1).ToString();
                im2.direction_id = im.direction_id;
                im2.time = im.time;
                im2.weight_per_meter_square = im.weight_per_meter_square;
                im2.coefficient = im.coefficient;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return im2;
        }
        #endregion

        #region Glass Library
        public static void addGlass(tb_radiation_characteristics rc)
        {
            try
            {
                tb_radiation_characteristics rc2 = LibraryManagerInCom.GlassCoeffientList.SingleOrDefault(i => i.glass_type == rc.glass_type);
                if (rc2 == null)
                {
                    eco.tb_radiation_characteristics.AddObject(rc);
                    eco.SaveChanges();
                    GlassCoeffientList.Add(rc);
                    MessageBox.Show("Add a new radiation characteristic item successfully!");
                }
                else
                {
                    MessageBox.Show("This item is already exist!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static void delGlass(tb_radiation_characteristics rc)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (result == MessageBoxResult.OK)
                {
                    eco.tb_radiation_characteristics.DeleteObject(rc);
                    eco.SaveChanges();
                    GlassCoeffientList.Remove(rc);
                    MessageBox.Show("Delete a radiation characteristic successfully!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static tb_radiation_characteristics copyGlass(tb_radiation_characteristics rc)
        {
            tb_radiation_characteristics rc2 = new tb_radiation_characteristics();
            try
            {
                rc2.glass_type = "Glass -- " + (LibraryManagerInCom.GlassCoeffientList.Count() + 1).ToString();
                rc2.glass_coefficient = rc.glass_coefficient;
                rc2.reflex_coefficient = rc.reflex_coefficient;
                rc2.penetration_coefficient = rc.penetration_coefficient;
                rc2.absorption_coefficient = rc.absorption_coefficient;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return rc2;
        }
        #endregion

        #region Window Sky Coefficient Library
        public static void addWindowSkywellCoefficient(tb_window_skywell ws)
        {
            try
            {
                tb_window_skywell ws2 = LibraryManagerInCom.WindowSkywellCoefficientList.SingleOrDefault(i => i.description == ws.description);
                if (ws2 == null)
                {
                    eco.tb_window_skywell.AddObject(ws);
                    eco.SaveChanges();
                    WindowSkywellCoefficientList.Add(ws);
                    if (ws.type == 1)
                    {
                        MessageBox.Show("Add a new window item successfully!");
                    }
                    else
                    {
                        MessageBox.Show("Add a new light well item successfully!");
                    }

                }
                else
                {
                    MessageBox.Show("This item is already exist!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public static void delWindowSkyWell(tb_window_skywell ws)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (result == MessageBoxResult.OK)
                {
                    eco.tb_window_skywell.DeleteObject(ws);
                    eco.SaveChanges();
                    WindowSkywellCoefficientList.Remove(ws);
                    if (ws.type == 1)
                    {
                        MessageBox.Show("Delete a window item successfully!");
                    }
                    else
                    {
                        MessageBox.Show("Delete a light well item successfully!");
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static tb_window_skywell copyWindowSkyWell(tb_window_skywell ws)
        {
            tb_window_skywell ws2 = new tb_window_skywell();
            try
            {
                if (ws.type == 1)
                {
                    ws2.description = "Window -- " + (LibraryManagerInCom.WindowSkywellCoefficientList.Count() + 1).ToString();
                }
                else
                {
                    ws2.description = "LightWell -- " + (LibraryManagerInCom.WindowSkywellCoefficientList.Count() + 1).ToString();
                }
                ws2.type = ws.type;
                ws2.distance = ws.distance;
                ws2.structure_characteristic = ws.structure_characteristic;
                ws2.coefficient = ws.coefficient;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return ws2;
        }
        #endregion

        #region Wood Door Glass Library
        public static void addWoodDoorWallGlass(tb_wood_door_heat_coefficient wd)
        {
            try
            {
                tb_wood_door_heat_coefficient wd2 = LibraryManagerInCom.WoodGlassCoefficientList.SingleOrDefault(i => i.description == wd.description);
                if (wd2 == null)
                {
                    eco.tb_wood_door_heat_coefficient.AddObject(wd);
                    eco.SaveChanges();
                    WoodGlassCoefficientList.Add(wd);
                    if (wd.type == 1)
                    {
                        MessageBox.Show("Add a new wall glass item successfully!");
                    }
                    else
                    {
                        MessageBox.Show("Add a new wood door item successfully!");
                    }

                }
                else
                {
                    MessageBox.Show("This item is already exist!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public static void delWoodDoorWallGlass(tb_wood_door_heat_coefficient wd)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (result == MessageBoxResult.OK)
                {
                    eco.tb_wood_door_heat_coefficient.DeleteObject(wd);
                    eco.SaveChanges();
                    WoodGlassCoefficientList.Remove(wd);
                    if (wd.type == 1)
                    {
                        MessageBox.Show("Delete a wall glass item successfully!");
                    }
                    else
                    {
                        MessageBox.Show("Delete a wood door item successfully!");
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static tb_wood_door_heat_coefficient copyWoodDoorWallGlass(tb_wood_door_heat_coefficient wd)
        {
            tb_wood_door_heat_coefficient wd2 = new tb_wood_door_heat_coefficient();
            try
            {
                if (wd.type == 1)
                {
                    wd2.description = "WallGlass -- " + (LibraryManagerInCom.WoodGlassCoefficientList.Count() + 1).ToString();
                }
                else
                {
                    wd2.description = "WoodDoor -- " + (LibraryManagerInCom.WoodGlassCoefficientList.Count() + 1).ToString();
                }
                wd2.type = wd.type;
                wd2.characteristic = wd.characteristic;

                wd2.coefficient = wd.coefficient;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return wd2;
        }
        #endregion

        #region Ceiling Library
        public static void addCeiling(tb_ceiling cl)
        {
            try
            {
                eco.tb_ceiling.AddObject(cl);
                eco.SaveChanges();
                CeilingList.Add(cl);
                MessageBox.Show("Add a ceiling item successfully!");
            }
            catch (Exception e)
            {

                MessageBox.Show(e.Message);
            }

        }

        public static void deleCeiling(tb_ceiling cl)
        {
            try
            {
                eco.tb_ceiling.DeleteObject(cl);
                eco.SaveChanges();
                CeilingList.Remove(cl);
                MessageBox.Show("Delete ceiling item successfully!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static tb_ceiling copyCeiling(tb_ceiling cl)
        {
            tb_ceiling cl2 = new tb_ceiling();
            cl2.description = cl.description;

            return cl2;
        }
        #endregion

        #region Ceiling Layer Library
        public static void addCeilingLayer(tb_ceiling_layer cl)
        {
            eco.tb_ceiling_layer.AddObject(cl);
            eco.SaveChanges();
            CeilingLayerList.Add(cl);
            MessageBox.Show("Add a ceiling layer successfully!");
        }

        public static void delCeilingLayer(tb_ceiling_layer clt)
        {
            eco.tb_ceiling_layer.DeleteObject(clt);
            eco.SaveChanges();
            CeilingLayerList.Remove(clt);
            MessageBox.Show("Delete a ceiling layer successfuly!");
        }
        public static tb_ceiling_layer copyCeilingLayer(tb_ceiling_layer clt)
        {
            tb_ceiling_layer clt2 = new tb_ceiling_layer();
            clt2.layer_type = clt.layer_type;
            CeilingLayerList.Add(clt2);
            return clt2;
        }
        #endregion

        #region Ceiling Heat Library
        public static void addCeilingHeat(tb_heat_transfer_ceiling htc)
        {
            tb_heat_transfer_ceiling h = CeilingCoefficientList.SingleOrDefault(i => i.ceiling_id == htc.ceiling_id && i.layer_id == htc.layer_id);
            if (h == null)
            {
                eco.tb_heat_transfer_ceiling.AddObject(htc);
                eco.SaveChanges();
                CeilingCoefficientList.Add(htc);
                MessageBox.Show("Add a ceiling heat transfer coefficient successfull!");
            }
            else
            {
                MessageBox.Show("This item is already available!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            }

        }
        public static void delCeilingHeat(tb_heat_transfer_ceiling htc)
        {
            try
            {
                tb_heat_transfer_ceiling h = CeilingCoefficientList.SingleOrDefault(i => i.ceiling_id == htc.layer_id && i.layer_id == htc.ceiling_id);
                if (h != null)
                {
                    eco.tb_heat_transfer_ceiling.DeleteObject(h);
                    eco.SaveChanges();
                    CeilingCoefficientList.Remove(h);
                    MessageBox.Show("Delete a ceiling heat transfer coefficient!");
                }
                else
                {
                    MessageBox.Show("The input value is not available!");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
        public static tb_heat_transfer_ceiling copyCeilingHeat(tb_heat_transfer_ceiling htc)
        {

            return htc;
        }
        #endregion

        #region Space Library
        public static void addAirConditionSpace(tb_air_conditioning_place ap)
        {
            try
            {
                tb_air_conditioning_place ap2 = AirConditionPlaceList.SingleOrDefault(i => i.description == ap.description);
                if (ap2 == null)
                {
                    eco.tb_air_conditioning_place.AddObject(ap);
                    eco.SaveChanges();
                    AirConditionPlaceList.Add(ap);
                    MessageBox.Show("Add a new air condition place successfully!");
                }
                else
                {
                    MessageBoxResult result = MessageBox.Show("This item is already exist. \n Do you want to overwrite it?", "Warning", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                    if (result == MessageBoxResult.OK)
                    {
                        eco.tb_air_conditioning_place.DeleteObject(ap2);
                        eco.tb_air_conditioning_place.AddObject(ap);
                        eco.SaveChanges();
                        AirConditionPlaceList.Remove(ap2);
                        AirConditionPlaceList.Add(ap);
                        MessageBox.Show("Update a new air condition place successfully!");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        public static void delAirConditionPlace(tb_air_conditioning_place ap)
        {
            try
            {
                eco.tb_air_conditioning_place.DeleteObject(ap);
                eco.SaveChanges();
                AirConditionPlaceList.Remove(ap);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        public static tb_air_conditioning_place copyAirCondionPlace(tb_air_conditioning_place ap)
        {
            tb_air_conditioning_place ap2 = new tb_air_conditioning_place(); ;

            try
            {
                ap2.impact_lighting = ap.impact_lighting;
                ap2.impact_people = ap.impact_people;
                ap2.air_amount_per_perople = ap.air_amount_per_perople;
                ap2.people_density = ap.people_density;
                ap2.description = "Space -- " + (AirConditionPlaceList.Count() + 1).ToString();

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            return ap2;
        }
        #endregion

        #region Air Flow Library
        public static void addExitingAir(tb_exiting_air ex)
        {
            eco.tb_exiting_air.AddObject(ex);
            eco.SaveChanges();
            ExitAirList.Add(ex);
            MessageBox.Show("Add a new exiting air item successfully!");
        }
        public static void delExitingAir(tb_exiting_air ex)
        {
            eco.tb_exiting_air.DeleteObject(ex);
            eco.SaveChanges();
            ExitAirList.Remove(ex);
            MessageBox.Show("Delete a new exiting air item successfully!");

        }
        public static tb_exiting_air copyExitingAir(tb_exiting_air ex1)
        {
            tb_exiting_air ex2 = new tb_exiting_air();
            ex2.room_volume = ex1.room_volume;
            ex2.coefficient = ex1.coefficient;
            ex2.description = "Exiting Air -- " + (eco.tb_exiting_air.Count() + 1);

            return ex2;

        }
        #endregion

        #region Wall Library
        public static void addWallMaterial(tb_heat_resistance hr)
        {
            eco.tb_heat_resistance.AddObject(hr);
            eco.SaveChanges();
            WallMaterialList.Add(hr);
            MessageBox.Show("Add a heat resistance item successfully!");
        }
        public static void delWallMaterial(tb_heat_resistance hr)
        {
            eco.tb_heat_resistance.DeleteObject(hr);
            eco.SaveChanges();
            WallMaterialList.Remove(hr);
            MessageBox.Show("Delete a heat resistance item successfully");
        }
        public static tb_heat_resistance copyWallMaterial(tb_heat_resistance hr)
        {
            tb_heat_resistance hr2 = new tb_heat_resistance();
            hr2.material_name = hr.material_name;
            hr2.density = hr.density;
            hr2.thickness = hr.thickness;
            hr2.heat_resistance = hr.heat_resistance;
            hr2.description = "Heat Resistance -- " + (eco.tb_heat_resistance.Count() + 1);

            return hr2;
        }
        #endregion

        #region Roof Library
        public static void addRoofSurface(tb_roof_surface rs)
        {
            eco.tb_roof_surface.AddObject(rs);
            eco.SaveChanges();
            RoofSurfaceList.Add(rs);
            MessageBox.Show("Add a roof surface item successfully!");
        }

        public static void delRoofSurface(tb_roof_surface rs)
        {
            eco.tb_roof_surface.DeleteObject(rs);
            eco.SaveChanges();
            RoofSurfaceList.Remove(rs);
            MessageBox.Show("Delete a roof surface item successfully!");
        }

        public static tb_roof_surface copyRoofSurface(tb_roof_surface rs)
        {
            tb_roof_surface rs2 = new tb_roof_surface();
            rs2.absorption_coefficient = rs.absorption_coefficient;
            rs2.surface_roof = "Roof Surface -- " + (eco.tb_roof_surface.Count() + 1);

            return rs2;
        }
        #endregion

        #region Roof Characteristic Library
        public static int addRoofCeilingCharacteristic(tb_roof_ceiling_characteristic ch)
        {
            int itemID = 0;

            try
            {
                tb_roof_ceiling_characteristic ch2 = LibraryManagerInCom.RoofCeilingCharacteristicList.SingleOrDefault(i => i.roof_ceiling_characteristic == ch.roof_ceiling_characteristic);
                if (ch2 == null)
                {
                    eco.tb_roof_ceiling_characteristic.AddObject(ch);
                    eco.SaveChanges();
                    RoofCeilingCharacteristicList.Add(ch);
                    MessageBox.Show("Add a roof ceiling characteristic item successfully!");
                    tb_roof_ceiling_characteristic ch3 = LibraryManagerInCom.RoofCeilingCharacteristicList.SingleOrDefault(i => i.roof_ceiling_characteristic == ch.roof_ceiling_characteristic);
                    itemID = int.Parse(ch3.id.ToString());
                }
                else
                {
                    MessageBox.Show("This item is already exist!");

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return itemID;
        }
        #endregion

        #region Roof Structure Library
        public static int addRoofCeilingStructure(tb_roof_ceiling_structure st)
        {
            int itemID = 0;

            try
            {
                tb_roof_ceiling_structure st2 = LibraryManagerInCom.RoofCeilingStructureList.SingleOrDefault(i => i.roof_ceiling_structure == st.roof_ceiling_structure);
                if (st2 == null)
                {
                    eco.tb_roof_ceiling_structure.AddObject(st);
                    eco.SaveChanges();
                    RoofCeilingStructureList.Add(st2);
                    MessageBox.Show("Add a roof ceiling structure item successfully!");
                    itemID = int.Parse(LibraryManagerInCom.RoofCeilingStructureList.SingleOrDefault(i => i.roof_ceiling_structure == st.roof_ceiling_structure).id.ToString());
                }
                else
                {
                    MessageBox.Show("This item is already exist!");

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return itemID;
        }
        #endregion

        #region Roof Coefficient Library
        public static void addRoofCeilingCoefficient(tb_roof_ceiling_heat_coefficient co)
        {
            try
            {
                tb_roof_ceiling_heat_coefficient co2 = LibraryManagerInCom.RoofCeilingHeatList.SingleOrDefault(i => i.characteristic_id == co.characteristic_id && i.structure_id == co.structure_id);
                if (co2 == null)
                {
                    eco.tb_roof_ceiling_heat_coefficient.AddObject(co);
                    eco.SaveChanges();
                    RoofCeilingHeatList.Add(co);
                    MessageBox.Show("Add a roof ceiling heat coefficient successfully!");
                }
                else
                {
                    MessageBox.Show("This item is already exist!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

        #region Equipment Library
        public static void addEquipment(tb_equipment eq)
        {
            try
            {
                tb_equipment e2 = LibraryManagerInCom.EquipmentList.SingleOrDefault(i => i.equipment_name == eq.equipment_name);
                if (e2 == null)
                {
                    eco.tb_equipment.AddObject(eq);
                    eco.SaveChanges();
                    EquipmentList.Add(eq);
                    MessageBox.Show("Add a new equipment item successfully!");

                }
                else
                {
                    MessageBox.Show("This item is already exist!", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        public static void delEquipment(tb_equipment eq)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (result == MessageBoxResult.OK)
                {
                    eco.tb_equipment.DeleteObject(eq);
                    eco.SaveChanges();
                    EquipmentList.Remove(eq);
                    MessageBox.Show("Delete a equipment item successfully");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static tb_equipment copyEquipment(tb_equipment eq)
        {
            tb_equipment eq2 = new tb_equipment();
            try
            {
                eq2.equipment_name = "Equipment -- " + (EquipmentList.Count() + 1).ToString();
                eq2.energy = eq.energy;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return eq2;
        }
        #endregion

        #endregion

        #region THERMAL EQUIPMENT

        #region AIR COEFFICIENTS
        // them 1 doi tuong Air Coefficient
        public static void addAirCoefficient(tb_air_coefficients ac)
        {
            tb_air_coefficients ac1 = AirCoefficientsList.SingleOrDefault(i => i.Temperature == ac.Temperature);
            if (ac1 == null)
            {
                eco.tb_air_coefficients.AddObject(ac);
                eco.SaveChanges();
                AirCoefficientsList.Add(ac);
                MessageBox.Show("Add new a air coefficient successfully!");

            }
            else
            {
                MessageBox.Show("This item is alreasy exist!");

            }
        }

        // Xoa 1 doi tuong Air Coeficient
        // Nếu ko còn đối tượng nào để xóa, sẽ hiện thông báo cho người dùng

        public static void delAirCoefficient(tb_air_coefficients ac)
        {
            if (eco.tb_air_coefficients == null)
            {
                MessageBox.Show("No air coefficient item is available");

            }
            else
            {
                eco.tb_air_coefficients.DeleteObject(ac);
                eco.SaveChanges();
                AirCoefficientsList.Remove(ac);
                MessageBox.Show("Delete a air coefficient successfully");

            }
        }
        // Copy 1 doi tuong Air Coefficient
        public static tb_air_coefficients copyAirCoefficient(tb_air_coefficients ac)
        {
            tb_air_coefficients ac2 = new tb_air_coefficients();
            ac2.Temperature = 0;
            ac2.Density = ac.Density;
            ac2.DynamicViscosity = ac.DynamicViscosity;
            ac2.HeatConductivity = ac.HeatConductivity;
            ac2.HeatSpecific = ac.HeatSpecific;
            ac2.KinematicViscosity = ac.KinematicViscosity;
            ac2.Prandtlcoefficient = ac.Prandtlcoefficient;

            return ac2;
        }


        #endregion

        #region WARER COEFFICIENTS
        // them 1 doi tuong Water Coefficient
        public static void addWaterCoefficient(tb_water_coefficients wc)
        {
            tb_water_coefficients ac1 = WaterCoefficientsList.SingleOrDefault(i => i.Temperature == wc.Temperature);
            if (ac1 == null)
            {
                eco.tb_water_coefficients.AddObject(wc);
                eco.SaveChanges();
                WaterCoefficientsList.Add(wc);
                MessageBox.Show("Add new a water coefficient successfully!");

            }
            else
            {
                MessageBox.Show("This item is alreasy exist!");

            }
        }

        // Xoa 1 doi tuong Water Coeficient

        public static void delWaterCoefficient(tb_water_coefficients wc)
        {
            if (eco.tb_water_coefficients == null)
            {
                MessageBox.Show("No water coefficient item is available");

            }
            else
            {
                eco.tb_water_coefficients.DeleteObject(wc);
                eco.SaveChanges();
                WaterCoefficientsList.Remove(wc);
                MessageBox.Show("Delete a water coefficient successfully");

            }
        }
        // Copy 1 doi tuong Water Coefficient
        public static tb_water_coefficients copyWaterCoefficient(tb_water_coefficients wc)
        {
            tb_water_coefficients ac2 = new tb_water_coefficients();
            ac2.Temperature = 0;
            ac2.Density01 = wc.Density01;
            ac2.Density02 = wc.Density02;
            ac2.DynamicViscosity = wc.DynamicViscosity;
            ac2.HeatConductivity = wc.HeatConductivity;
            ac2.HeatSpecific = wc.HeatSpecific;
            ac2.KinematicViscosity = wc.KinematicViscosity;
            ac2.Prandtl_Coefficient = wc.Prandtl_Coefficient;
            ac2.IncreasedEvaporationCoefficient = wc.IncreasedEvaporationCoefficient;


            return ac2;
        }


        #endregion


        #endregion

        #region UNPROCESS
        public static void addCSWallMaterial(tb_wall_material wm)
        {
            eco.tb_wall_material.AddObject(wm);
            eco.SaveChanges();
        }

        public static void addHeatByMan(tb_heat_by_man hym)
        {
            try
            {
                tb_heat_by_man hym2 = HeatByManList.SingleOrDefault(i => i.working_place_id == hym.working_place_id && i.working_place_temperature == hym.working_place_temperature);
                if (hym2 == null)
                {
                    eco.tb_heat_by_man.AddObject(hym);
                    eco.SaveChanges();
                    MessageBox.Show("Add a new heat by man item successfully!");
                }
                else
                {
                    MessageBoxResult result = MessageBox.Show("This item is already exist \n Do you want overwrite it?", "Warning", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                    if (result == MessageBoxResult.OK)
                    {
                        eco.tb_heat_by_man.DeleteObject(hym2);
                        eco.tb_heat_by_man.AddObject(hym);
                        eco.SaveChanges();
                        MessageBox.Show("Update a heat by man item successfully!");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public static void delHeatByMan(tb_heat_by_man hym)
        {
            try
            {
                MessageBoxResult r = MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                if (r == MessageBoxResult.OK)
                {
                    eco.tb_heat_by_man.DeleteObject(hym);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        public static tb_heat_by_man copyHeatByMan(tb_heat_by_man hym)
        {
            tb_heat_by_man hym2 = new tb_heat_by_man();
            try
            {

                hym2.working_place_id = hym.working_place_id;
                hym2.working_place_temperature = hym.working_place_temperature;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }

            return hym2;
        }
        #endregion
        #endregion

        #region Module 2
        public static void loadWaterCoefficientsList()
        {
            WaterCoefficientsList = new ObservableCollection<tb_water_coefficients>(eco.tb_water_coefficients.ToList());
        }

        public static void loadAirCoefficientsList()
        {
            AirCoefficientsList = new ObservableCollection<tb_air_coefficients>(eco.tb_air_coefficients.ToList());
        }
    }
        #endregion
}

